package com.example.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author ZHAO Yudong
 * @Date 2021/12/22 17:47
 * @description：我的过滤器
 */
@Slf4j
public class MyFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("-----------------------------------");
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String url;
        String requestURL = request.getRequestURL().toString();
        String queryString = request.getQueryString();
        if (StringUtils.isNotEmpty(queryString)){
            url=requestURL+"?"+queryString;
        }else{
            url=requestURL;
        }
        log.info("requesrUrl={}",url);
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
